//
//  ZBDrawerTransition.m
//  ZBSlideMemuManger
//
//  Created by apple on 2018/5/31.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZBDrawerTransition.h"
#import "ZBTransitionMaskView.h"

@interface ZBDrawerTransition()
@property (nonatomic,weak) ZBSlideMemuMangerConfiguration *configuration;

@property (nonatomic,assign) ZBDrawerTransitionType transitionType;

@property (nonatomic,assign) ZBDrawerAnimationType  animationType;

@property (nonatomic,assign) NSTimeInterval  hiddenDelayTime;


@end

@implementation ZBDrawerTransition
+ (instancetype)transitionWithType:(ZBDrawerTransitionType)transitionType animationType:(ZBDrawerAnimationType)animationType configuration:(ZBSlideMemuMangerConfiguration *)configuration
{
    return [[ZBDrawerTransition alloc]initWithTransitionType:transitionType animationType:animationType configuration:configuration];
}
- (instancetype)initWithTransitionType:(ZBDrawerTransitionType)transitionType animationType:(ZBDrawerAnimationType)animationType configuration:(ZBSlideMemuMangerConfiguration *)configuration
{
    if(self = [super init])
    {
        self.transitionType = transitionType;
        self.animationType = animationType;
        self.configuration = configuration;
        if (transitionType == ZBDrawerTransitionType_Hidden)
            [self setupHiddenAnimationDelayTime];
    }
    return self;
}
- (void)setupHiddenAnimationDelayTime {
    _hiddenDelayTime = 0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
        _hiddenDelayTime = 0.03;
    }
}
#pragma mark - UIViewControllerAnimatedTransitioning
//转场时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    NSTimeInterval transitionDuration = 0;
    switch (self.transitionType) {
        case ZBDrawerTransitionType_Show:
            transitionDuration = self.configuration.showAnimDuration;
            break;
        case ZBDrawerTransitionType_Hidden:
            transitionDuration = self.configuration.HiddenAnimDuration;
            break;
            
        default:
            break;
    }
    return transitionDuration;
}

//转场效果
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    switch (self.transitionType) {
        case ZBDrawerTransitionType_Show:
            [self animationViewShow:transitionContext];
            break;
        case ZBDrawerTransitionType_Hidden:
            [self animationViewHidden:transitionContext];
            break;
        default:
            break;
    }
}


#pragma mark - private methods
- (void)animationViewShow:(id <UIViewControllerContextTransitioning>)transitionContext {
//    if (self.animationType == ZBDrawerAnimationType_Default) {
//        [self defaultAnimationWithContext:transitionContext];
//    }else if (self.animationType == ZBDrawerAnimationType_Mask) {
//        [self maskAnimationWithContext:transitionContext];
//    }else {
//
//    }
    switch (self.animationType) {
        case ZBDrawerAnimationType_Default:
             [self defaultAnimationWithContext:transitionContext];
            break;
        case ZBDrawerAnimationType_Mask:
            [self maskAnimationWithContext:transitionContext];
            break;
            
        default:
            break;
    }
}

- (void)defaultAnimationWithContext:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    ZBTransitionMaskView *maskView = [ZBTransitionMaskView shareInstance];
    maskView.frame = fromVC.view.bounds;
    [fromVC.view addSubview:maskView];
    UIView *containerView = [transitionContext containerView];
    
    UIImageView *imageV;
    if (self.configuration.backImage) {
        imageV = [[UIImageView alloc] initWithFrame:containerView.bounds];
        imageV.image = self.configuration.backImage;
        imageV.transform = CGAffineTransformMakeScale(1.4, 1.4);
        imageV.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    }
    [containerView addSubview:imageV];
    
    CGFloat width = self.configuration.distance;
    CGFloat x = - width / 2;
    CGFloat ret = 1;
    if (self.configuration.direction == ZBDrawerTransitionDirection_Right) {
        x = kZBSCREENWIDTH - width / 2;
        ret = -1;
    }
    toVC.view.frame = CGRectMake(x, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame));
    [containerView addSubview:toVC.view];
    [containerView addSubview:fromVC.view];
    // 计算缩放后需要平移的距离
    CGFloat translationX = width - (kZBSCREENWIDTH * (1 - self.configuration.scaleY) / 2);
    CGAffineTransform t1 = CGAffineTransformMakeScale(self.configuration.scaleY, self.configuration.scaleY);
    CGAffineTransform t2 = CGAffineTransformMakeTranslation(ret * translationX, 0);
    CGAffineTransform fromVCTransform = CGAffineTransformConcat(t1, t2);
    CGAffineTransform toVCTransform;
    if (self.configuration.direction == ZBDrawerTransitionDirection_Right) {
        toVCTransform = CGAffineTransformMakeTranslation(ret * (x - CGRectGetWidth(containerView.frame) + width), 0);
    }else {
        toVCTransform = CGAffineTransformMakeTranslation(ret * width / 2, 0);
    }
    
    [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:0 animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:1.0 animations:^{
            
            fromVC.view.transform = fromVCTransform;
            toVC.view.transform = toVCTransform;
            imageV.transform = CGAffineTransformIdentity;
            maskView.alpha = self.configuration.maskAlpha;
            
        }];
        
    } completion:^(BOOL finished) {
        if (![transitionContext transitionWasCancelled]) {
            maskView.userInteractionEnabled = YES;
            maskView.toViewSubViews = fromVC.view.subviews;
            [transitionContext completeTransition:YES];
            [containerView addSubview:fromVC.view];
        }else {
            [imageV removeFromSuperview];
            [ZBTransitionMaskView releaseInstance];
            [transitionContext completeTransition:NO];
        }
    }];
}

- (void)maskAnimationWithContext:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    ZBTransitionMaskView *maskView = [ZBTransitionMaskView shareInstance];
    maskView.frame = fromVC.view.bounds;
    [fromVC.view addSubview:maskView];
    
    UIView *containerView = [transitionContext containerView];
    
    CGFloat width = self.configuration.distance;
    CGFloat x = - width;
    CGFloat ret = 1;
    if (self.configuration.direction == ZBDrawerTransitionDirection_Right) {
        x = kZBSCREENWIDTH;
        ret = -1;
    }
    toVC.view.frame = CGRectMake(x, 0, width, CGRectGetHeight(containerView.frame));
    
    [containerView addSubview:fromVC.view];
    [containerView addSubview:toVC.view];
    
    CGAffineTransform toVCTransiform = CGAffineTransformMakeTranslation(ret * width , 0);
    
    [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:0 animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:1.0 animations:^{
            toVC.view.transform = toVCTransiform;
            maskView.alpha = self.configuration.maskAlpha;
        }];
        
    } completion:^(BOOL finished) {
        
        if (![transitionContext transitionWasCancelled]) {
            [transitionContext completeTransition:YES];
            maskView.toViewSubViews = fromVC.view.subviews;
            [containerView addSubview:fromVC.view];
            [containerView bringSubviewToFront:toVC.view];
            maskView.userInteractionEnabled = YES;
        }else {
            [ZBTransitionMaskView releaseInstance];
            [transitionContext completeTransition:NO];
        }
    }];
}

- (void)animationViewHidden:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    ZBTransitionMaskView *maskView = [ZBTransitionMaskView shareInstance];
    for (UIView *view in toVC.view.subviews) {
        if (![maskView.toViewSubViews containsObject:view]) {
            [view removeFromSuperview];
        }
    }
    UIView *containerView = [transitionContext containerView];
    UIImageView *backImageView;
    if ([containerView.subviews.firstObject isKindOfClass:[UIImageView class]])
        backImageView = containerView.subviews.firstObject;
    
    [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:self.hiddenDelayTime options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1.0 animations:^{
            toVC.view.transform = CGAffineTransformIdentity;
            fromVC.view.transform = CGAffineTransformIdentity;
            maskView.alpha = 0;
            backImageView.transform = CGAffineTransformMakeScale(1.4, 1.4);
        }];
        
    } completion:^(BOOL finished) {
        if (![transitionContext transitionWasCancelled]) {
            maskView.toViewSubViews = nil;
            [ZBTransitionMaskView releaseInstance];
            [backImageView removeFromSuperview];
        }
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
    }];
    
}

@end
